package com.xsq.app.service;

import com.xsq.app.dao.DockerDao;
import com.xsq.app.model.Docker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @ClassName DockerImpl
 * @Description TODO
 * @Author xsq
 * @Date 2021/1/22 17:08
 **/
@Service
public class DockerImpl {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DockerDao dockerDao;

    public Docker add(String name) {
        Docker docker = new Docker();
        docker.setName(name);
        docker = dockerDao.save(docker);
        redisTemplate.opsForValue().set("docker"+docker.getId(),docker);
       return docker;
    }

    public Docker get(Long id) {
        Object object = redisTemplate.opsForValue().get("docker" + id);
        if(object != null){
            return (Docker)object;
        }
        return dockerDao.findById(id).get();
    }
}
